package com.fau.wm.action.index;

import java.util.HashMap;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.fau.wm.action.PublicAction;
import com.fau.wm.entity.MedicalAdmin;
import com.fau.wm.entity.MedicalLogistics;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;


/**
 * @desc 物流管理Action
 * @author hufq
 * @datetime 2014年9月24日,下午4:50:13
 */
@Controller
@Scope("prototype")
@ParentPackage("default")
@Namespace("/")
public class LogisticsAction extends PublicAction implements ModelDriven<MedicalLogistics>{

	private static final long serialVersionUID = -2218309323101013121L;
	private MedicalLogistics logistics = new MedicalLogistics();
	
	/**
	 * @desc 初始化物流信息管理页面
	 * @return
	 * @author hufq
	 * @datetime 2014年9月28日,上午9:16:23
	 */
	@Action(value = "logistics_init", results = {@Result(name = SUCCESS, location = "logistics_init.jsp")})
	public String init(){
		return SUCCESS;
	}
	
	/**
	 * @desc 加载物流列表数据
	 * @return
	 * @author hufq
	 * @datetime 2014年9月24日,下午5:03:14
	 */
	@Action(value = "logistic_view")
	public String view(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = ActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		logistics.setHospitalId(admin.getEnterpriseId());
		pageBean = this.baseAllService.getLogisticsManagerService().findLogisticsByPage(logistics, map);
		this.map.clear();
		map.put("total", this.pageBean.getTotalRow());
		map.put("data", this.pageBean.getResult());
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 添加物流公司
	 * @return
	 * @author hufq
	 * @datetime 2014年9月25日,下午3:26:43
	 */
	@Action(value = "logistics_add", results = {@Result(name = SUCCESS, location = "logistics_add.jsp")})
	public String add(){
		return SUCCESS;
	}
	
	/**
	 * @desc 保存物流公司信息
	 * @return
	 * @author hufq
	 * @datetime 2014年9月25日,下午4:05:51
	 */
	@Action(value = "logistics_save")
	public String save(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		Boolean flag = this.baseAllService.getLogisticsManagerService().saveLogistics(logistics, admin);
		if(flag){
			map.put("success", true);
		}else{
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc  删除物流公司信息
	 * @return null
	 * @author hufq
	 * @datetime 2014年9月28日,上午9:13:21
	 */
	@Action(value = "logistics_del")
	public String del(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(logistics.getId())){
			this.baseAllService.getLogisticsManagerService().deleteLogistics(logistics);
			map.put("success", true);
			formatJSON(response, map);
		}
		return null;
	}
	
	/**
	 * @desc 编辑物流公司信息
	 * @return
	 * @author hufq
	 * @datetime 2014年9月28日,上午9:13:54
	 */
	@Action(value = "logistics_edit", results = {@Result(name = SUCCESS, location = "logistics_edit.jsp")})
	public String edit(){
		return SUCCESS;
	}
	
	/**
	 * @desc 编辑保存物流公司信息
	 * @return
	 * @author hufq
	 * @datetime 2014年9月28日,上午9:14:30
	 */
	@Action(value = "logistics_save_edit")
	public String editSave(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(logistics.getId())){
			this.baseAllService.getLogisticsManagerService().saveEditLogistics(logistics);
			map.put("success", true);
		}else{
			map.put("message", "error");
			}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 显示物流公司详细信息
	 * @return
	 * @author hufq
	 * @datetime 2014年9月28日,上午9:15:34
	 */
	@Action(value = "logistics_view", results = {@Result(name = SUCCESS, location = "logistics_data_view.jsp")})
	public String viewDetail(){
		return SUCCESS;
	}
	
	@Override
	public MedicalLogistics getModel() {
		return logistics;
	}

}